const express = require('express')
const {
  createAccount,
  queryAccount,
  deleteAccount,
  queryByName,
  findAccount,
} = require('../../services')
const router = express.Router()

//注册账号
router.post('/register', async (req, res) => {
  console.log('register')
  let { name, password, juri } = req.body
  let result = await createAccount({ name, password, juri })
  res.json(result)
})
//登录账号
router.post('/login', async (req, res) => {
  let { name, password } = req.body
  let result = await findAccount({ name, password })
  if (result.status === 1) {
    res.json({
      code: 200,
      message: 'login success',
      juri: result.juri, // 0: 普通用户, 1: 交管部门, 2: 管理员
      user: result.user,
      // token: result.token,
    })
  } else {
    res.status(401).json({ code: 401, message: result.msg })
  }
})

//查询账号
router.get('/', async (req, res) => {
  let result = await queryAccount()
  res.json(result)
})
//名字查询账号
router.get('/name', async (req, res) => {
  let name = req.query.name
  let result = await queryByName(name)
  res.json(result)
})
//删除账号
router.delete('/:id', async (req, res) => {
  let id = req.params.id
  let result = await deleteAccount(id)
  res.json(result)
})
module.exports = router
